package org.lq.shopping.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.logging.Log;
import org.apache.log4j.Logger;
import org.lq.shopping.dao.ShopManagerDao;
import org.lq.shopping.entity.ShopManager;
import org.lq.shopping.util.DruidUtil;

public class ShopManagerDaoImpl implements ShopManagerDao {
	QueryRunner qr=new QueryRunner(DruidUtil.getDataSource());
	Logger log = Logger.getLogger(ShopManagerDaoImpl.class);
	@Override
	public int add(ShopManager t) {
		String sql = "insert into shop_manager(mname,mpassword,mregtime,memail,mid) values(?,?,?,?,manager_seq.nextval)";
		int num = 0;
		log.info("管理员-添加操作");
		try {
			num = qr.execute(sql, t.getMname(),t.getMpassword(),t.getMregtime(),t.getMemail());
			log.debug("sql:"+sql);
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		return num;
	}

	@Override
	public int delete(int id) {
		String sql = "delete shop_manager where mid = ?";
		int num = 0;
		log.info("管理员-删除");
		try {
			num = qr.execute(sql, id);
			log.debug("sql:"+sql);
			log.debug("参数："+id);
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		return num;
	}

	@Override
	public int update(ShopManager t) {
		String sql = "update shop_manager set mname = ?,mpassword = ?,mregtime = ?,memail = ? where mid = ?";
		int num = 0;
		log.info("管理员-修改");
		try {
			num = qr.execute(sql, t.getMname(),t.getMpassword(),t.getMregtime(),t.getMemail(),t.getMid());
			log.debug("sql:"+sql);
			log.debug("参数："+t.getMname()+","+t.getMpassword()+","+t.getMregtime()+","+t.getMemail()+","+t.getMid());
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		return num;
	}

	@Override
	public List<ShopManager> findAll() {
		String sql = "select * from shop_manager order by mid";
		List<ShopManager> list = null;
		log.info("管理员-查询所有");
		try {
			list = qr.query(sql, new BeanListHandler<>(ShopManager.class));
			log.debug("sql:"+sql);
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		return list;
	}

	@Override
	public List<ShopManager> LikeColumnByValue(String columName, Object value) {
		String sql = "select * from shop_manager where ? = ?";
		List<ShopManager> list = null;
		log.info("管理员-模糊查询");
		try {
			list = qr.query(sql, new BeanListHandler<>(ShopManager.class),columName,value);
			log.debug("sql:"+sql);
			log.debug("参数："+columName+","+value);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 0;
	}
	@Override
	public List<ShopManager> findPage(int pageIndex, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ShopManager findByUname(String uname) {
		String sql = "select * from shop_manager where mname = ?";
		ShopManager sm = null;
		log.info("管理员-通过用户名查询");
		try {
			sm = qr.query(sql, new BeanHandler<>(ShopManager.class),uname);
			log.debug("sql:"+sql);
			log.debug("参数："+uname);
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		return sm;
	}

	@Override
	public ShopManager findById(int id) {
		String sql = "select * from shop_manager where mid = ?";
		ShopManager sm = null;
		log.info("管理员-通过id查询");
		try {
			sm = qr.query(sql, new BeanHandler<>(ShopManager.class),id);
			log.debug("sql:"+sql);
			log.debug("参数："+id);
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		return sm;
	}

	@Override
	public ShopManager Login(String account, String upass) {
		String sql = "select * from shop_manager where mname = ? and mpassword = ?"
				+ "or memail = ? and mpassword = ?";
		ShopManager sm = null;
		log.info("管理员-登录");
		try {
			sm = qr.query(sql, new BeanHandler<>(ShopManager.class),account,upass,account,upass);
			log.debug("sql:"+sql);
			log.debug("参数："+account+","+upass);
		} catch (SQLException e) {
			log.error(e.getMessage());
		}
		return sm;
	}

	@Override
	public int disableUserById(int userId) {
		int num  = 0;
		String sql = "update shop_user set user_state = 1 where user_id = ?";
		try {
			num = qr.execute(sql, userId);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return num;
	}

	@Override
	public boolean reUserPass(int userId) {
		// TODO Auto-generated method stub
		return false;
	}
	
	
}
